package com.yay.warehouse.tabs;

import com.yay.warehouse.Contact;
import com.yay.warehouse.IKeys;
import com.yay.warehouse.R;
import com.yay.warehouse.Step3;
import com.yay.warehouse.model.LFType;
import com.yay.warehouse.model.Ticket;
import com.yay.warehouse.service.L10N;
import com.yay.warehouse.service.TicketsService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class LFList extends Activity {
	private TicketsService ticketsService;
	private TicketsAdapter adapter;
	private Intent contact;
	private GridView gridView;
	private Intent step3;
	private Button btnNext;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        
        Ticket currentTicket = TicketsService.getCurrentTicket();
        TicketsAdapter.getTickets().clear();
        TicketsAdapter.reload = false;
        int type = currentTicket.getType() == LFType.LOST.getId() ? LFType.FOUND.getId() : LFType.LOST.getId();
        TicketsAdapter.getTickets().addAll(getTicketsService().find(type, currentTicket.getCategory(), currentTicket.getLatitude(), currentTicket.getLongitude()));
        
        initGridView();
        initBtnNext();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(TicketsAdapter.reload) {
			Ticket currentTicket = TicketsService.getCurrentTicket();
	        TicketsAdapter.getTickets().clear();
        	TicketsAdapter.reload = false;
        	int type = TicketsService.getCurrentTicket().getType() == LFType.LOST.getId() ? LFType.FOUND.getId() : LFType.LOST.getId();
        	TicketsAdapter.getTickets().addAll(getTicketsService().find(type, currentTicket.getCategory(), currentTicket.getLatitude(), currentTicket.getLongitude()));
    		getAdapter().notifyDataSetChanged();
		}
	}

	
	// PRIVATE METHODS
	
	private TicketsService getTicketsService() {
		if(ticketsService == null) {
			ticketsService = new TicketsService();
		}
		return ticketsService;
	}
	
	private TicketsAdapter getAdapter() {
		if(adapter == null) {
			adapter = new TicketsAdapter(this);
		}
		return adapter;
	}
	
	private Intent getContact() {
		if(contact == null) {
			contact = new Intent(LFList.this, Contact.class); 
		}
		return contact;
	}
	
	private void initGridView() {
		if(gridView == null) {
			gridView = (GridView) findViewById(R.id.gridTickets);
	        gridView.setAdapter(getAdapter());
	        gridView.setOnItemClickListener(new OnItemClickListener() {
	        	@Override
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        		getContact().putExtra(IKeys.TICKET, getAdapter().getItem(position));
	        		startActivity(getContact());
	            }
	        });
		}
	}
	
	private Intent getStep3() {
		if(step3 == null) {
			step3 = new Intent(LFList.this, Step3.class); 
		}
		return step3;
	}
	
	private void initBtnNext() {
	    if(btnNext == null) {
			btnNext = (Button) findViewById(R.id.btnNext);
		    btnNext.setTypeface(L10N.font);
	        btnNext.setOnClickListener(new OnClickListener() {
		    	 @Override
		    	 public void onClick(View arg0) {
		    		 startActivity(getStep3());
		    		 finish();
		    	 }
		    });
	    }
	}

}
